<?PHP
session_start();
include("../config/connect.php");
include("../public/common.php");

if( !isset($_SESSION["gs_user"]) ) 
{
	$url = "Location:login.php";
	header($url);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>购物兔</title>
<style type="text/css">
<!--
body,td,th {
	font-family: 宋体;
	font-size: 13px;
	color: #000;
}
body {
	background-color: #FFF;
}
-->
</style>
</head>
<body>
<?php
//$trade_id = 1;
$trade_id = CheckUrlParamNoDie("trade_id");
$szItem = "";
$tb_id = "";
$info = "";
$phone = "";
$all_return = 0;
$has_return = 0;
$b_return = 0;
$price = 0;
$last_id = 0;

$opt_add = CheckUrlParamNoDie("opt_add");
$item_id = CheckUrlParamNoDie("item_id");

//url from view_feedback.php
if( $opt_add == "1" )
{
	$id = SetUrlParamInt("id");
	$sql = "select * from feedback where id=$id limit 1";
	$result = mysql_query( $sql );
	if( $result && $row = mysql_fetch_assoc( $result ) )
	{
		$all_return = $row["price"];
		$tb_id = $row["tb_id"];	
		
		if( $tb_id != "" )
		{
			$sql = "select * from user where tb_id='$tb_id' limit 1";
			$result = mysql_query( $sql );
			if( $result && $row = mysql_fetch_assoc( $result ) )
			{
				$phone = $row["phone"];
			}
		}
	}
}
else if( $item_id != "" )
{
	$id = SetUrlParamInt("id");
	$sql = "select * from other_trade where id=$id limit 1";
	$result = mysql_query( $sql );
	if( $result && $row = mysql_fetch_assoc( $result ) )
	{
		$all_return = $row["price"];
		$tb_id = $row["tb_id"];	
		$szItem = $item_id;
		
		if( $tb_id != "" )
		{
			$sql = "select * from user where tb_id='$tb_id' limit 1";
			$result = mysql_query( $sql );
			if( $result && $row = mysql_fetch_assoc( $result ) )
			{
				$phone = $row["phone"];
			}
		}
	}
}



if( isset( $_POST["submit"] ) )
{
	$szItem = GetPostParamStr("item_id");
	$tb_id = GetPostParamStr("tb");
	$phone = GetPostParamStr("phone");
	$info = GetPostParamStr("info");
	$all_return = GetPostParamFloat("all_return");
	
	$has_return = GetPostParamFloat("has_return");
	$b_return = GetPostParamStr("b_return");
	$last_id = GetPostParamInt("last_id");
	$phone = GetPostParamStr("phone");
	
	if( $b_return == "on" )
		$b_return = 1;
	else
		$b_return = 0;
		
	$price = GetPostParamFloat("price");
	$trade_id = GetPostParamStr("trade");
	
	$sql = "select id from return_record where id=$last_id limit 1";
	
	$result = mysql_query( $sql );
	$row_num = mysql_num_rows( $result );
	
	if( $row_num == 0 )
	{
		$sql = "insert into return_record( tb_id, phone, all_return, has_return, b_return, price, item_id, trade_id, info ) values('$tb_id', '$phone', $all_return, $has_return, $b_return, $price, '$szItem', '$trade_id', '$info')";
		$result = mysql_query( $sql );
		$last_id = mysql_insert_id();
	}
	else
	{
		$sql = "update return_record set all_return=$all_return, price=$price, has_return=$has_return, b_return=$b_return, info='$info', trade_id='$trade_id', item_id='$szItem', phone='$phone', tb_id='$tb_id' where id=$last_id limit 1";
		$result = mysql_query( $sql );
	}
	
	echo "<script language=\"javascript\">	alert(\"保存成功\");</script>";
}
?>
<br />

<a href="view_other_trade.php">返回</a>
<form action="add_trade.php" method="post">
<table cellpadding="0" cellspacing="0" border="1" align="center">
	<tr>
    	<td width="60" height="30" align="center">物品ID：</td>
        <td colspan="5"><input type="text" name="item_id" value="<?PHP echo $szItem; ?>" size="70"/></td>
    </tr>
    <tr>
   	  <td width="60" height="30" align="center">订单号：</td>
      <td width="140" height="22"><input type="text" width="140" id="trade_id" name="trade" height="20" value="<?PHP echo $trade_id; ?>"  /></td>
      <td width="58" height="30" align="center">账号：</td>
        <td width="152" height="22"><input type="text" width="140" id="tb_id" name="tb" height="20" value="<?PHP echo $tb_id; ?>" /></td>
      <td width="55" height="30" align="center">手机号：</td>
      <td width="145" height="22"><input type="text" width="140" id="phone_id" name="phone" height="20" value="<?PHP echo $phone; ?>" /></td>
    </tr>
    <tr>
    	<td colspan="2" height="30" align="right"s>共需返还金额：</td>
        <td colspan="4"><input type="text"  width="340" id="all_return_id" name="all_return" height="20" value="<?PHP echo $all_return; ?>" />&nbsp;</td>
    </tr>
    <tr>
    	<td colspan="2" height="30"  align="right"s>成交额：</td>
        <td colspan="4"><input type="text"  width="340" id="price_id" name="price" height="20" value="<?PHP echo $price; ?>" />&nbsp;</td>
    </tr>
    <tr>
    	<td colspan="2" height="30" align="right"s>已返现：</td>
        <td colspan="4"><input type="text"  width="340" id="has_return_id" name="has_return" height="20" value="<?PHP echo $has_return; ?>" />&nbsp;</td>
    </tr>
    <tr>
    	<td colspan="2" height="30" align="right"s>是否返全：</td>
        <td colspan="4"><input type="checkbox" id="b_return_id" name="b_return" <?PHP if( $b_return == 1 )	echo "checked='checked'"; ?> />
        &nbsp;<input type="hidden" id="last_id" name="last_id" value="<?PHP echo $last_id; ?>" /></td>
    </tr>
    <tr>
    	<td colspan="2" height="30" align="right"s>备注：</td>
        <td colspan="4"><textarea name="info" cols="56" rows="7"><?PHP echo $info; ?></textarea></td>
    </tr>
    <tr>
    	<td colspan="6" height="30" align="center"><input type="submit" name="submit" value="保存" style=" width:80px; height:23px"/></td>
    </tr>
</table>
</form>
</body>
</html>